'use strict'

define(['jquery', 'bootstrap', 'select2', 'validate', 'validate.en', 'shiftform',], function($) {
  
  return {
        
    init: function(data) {
        this.remarks();
        this.transferCamp();
        this.zip();
        this.saveQrcode(data.code, data.grouped);
        this.group(data.code);
        this.changeGroup();
        this.editTag();
        this.onlineLink();
        this.transferClass();
    },
    
    remarks: function() {
        $(document).on('shown.bs.modal', '#online-round-remarks', function(e){
            
          $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
          
          var $form = $(e.currentTarget).find("form")                          //bind shiftform
          $form.shiftform({
            beforeSubmit : function(el) {
              $form.trigger("validate.ajax.submit");
              return $.bt_validate.result[$.bt_validate.form_id]
            }
          })

        })
      },
      
      transferCamp: function() {
          $(document).on('shown.bs.modal', '#online-round-transfer-camp', function(e){
              
            $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
            
            var $form = $(e.currentTarget).find("form")                          //bind shiftform
            $form.shiftform({
              beforeSubmit : function(el) {
                $form.trigger("validate.ajax.submit");
                return $.bt_validate.result[$.bt_validate.form_id]
              }
            })

          })
        },
      
      zip: function() {
          $(document).on('shown.bs.modal', '#online-round-zip', function(e){
              
            $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
            
            var $form = $(e.currentTarget).find("form")                          //bind shiftform
            $form.shiftform({
              beforeSubmit : function(el) {
                $form.trigger("validate.ajax.submit");
                return $.bt_validate.result[$.bt_validate.form_id]
              }
            })

          })
        },
        onlineLink: function() {
            $(document).on('shown.bs.modal', '#generate-courseware', function(e){
                
              $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
              
              var $form = $(e.currentTarget).find("form")                          //bind shiftform
              $form.shiftform({
                beforeSubmit : function(el) {
                  $form.trigger("validate.ajax.submit");
                  return $.bt_validate.result[$.bt_validate.form_id]
                }
              })

            })
          },
        
        saveQrcode: function(code, grouped) {
            $(document).on('shown.bs.modal', '#online-round-qrcode-upload', function(e){
                
              $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
              var $form = $(e.currentTarget).find("form")                          //bind shiftform
               $(e.currentTarget).find('button[type="submit"]').click(function(){
               
                 $form.trigger("validate.ajax.submit");
                 window.location.href = window.__CTX__.root + 'online-rounds/' + code + '?grouped=' + grouped;
        	   
               })

            })
          },
        
          group: function(code) {
        	  $(document).on('shown.bs.modal', '#online-round-group', function(e){
        		  
        		  $(e.currentTarget).find("form").shiftform({
        			  successRedirect : window.__CTX__.root + "/online-rounds/" + code + "?grouped=true"
        		  }) 
                  
                })
            },
       changeGroup:function() {
           $(document).on('shown.bs.modal', '#change-group, #edit-group, #batch-change-group', function(e){
               
               $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
               
               var $form = $(e.currentTarget).find("form")                          //bind shiftform
               $form.shiftform({
                 beforeSubmit : function(el) {
                   $form.trigger("validate.ajax.submit");
                   return $.bt_validate.result[$.bt_validate.form_id]
                 }
               })

             })
           },
        editTag:function() {
            $(document).on('shown.bs.modal', '#tag', function(e){
                
                $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
                
                var $form = $(e.currentTarget).find("form")                          //bind shiftform
                $form.shiftform({
                  beforeSubmit : function(el) {
                    $form.trigger("validate.ajax.submit");
                    return $.bt_validate.result[$.bt_validate.form_id]
                  }
                })

              })
            },

    transferClass: function() {
      $(document).on('shown.bs.modal', '#online-round-transfer-class', function(e){
          $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
          
          var $form = $(e.currentTarget).find("form")                          //bind shiftform
          $form.shiftform({
            beforeSubmit : function(el) {
              $form.trigger("validate.ajax.submit");
              return $.bt_validate.result[$.bt_validate.form_id]
            }
          })
      });
    }
  }
})
